#ifndef VECTOR_H
#define VECTOR_H

#include <stddef.h>
#include <stdint.h>

// 数据类型枚举
typedef enum {
    VECTOR_TYPE_INT,
    VECTOR_TYPE_FLOAT,
    VECTOR_TYPE_DOUBLE,
    VECTOR_TYPE_CHAR,
    VECTOR_TYPE_SHORT,
    VECTOR_TYPE_LONG,
    VECTOR_TYPE_LONGLONG,
    VECTOR_TYPE_UINT,
    VECTOR_TYPE_UCHAR,
    VECTOR_TYPE_USHORT,
    VECTOR_TYPE_ULONG,
    VECTOR_TYPE_ULONGLONG,
    VECTOR_TYPE_BOOL
} VectorType;

// Vector结构定义
typedef struct {
    void* data;         // 指向实际数据数组的指针
    VectorType type;    // 存储的数据类型
    size_t size;        // 当前元素数量
    size_t capacity;    // 当前容量
    size_t element_size;// 单个元素大小
} Vector;

// 创建新的Vector
Vector* VectorNew(VectorType type, size_t initial_capacity);

// 释放Vector内存
void VectorFree(Vector* vector);

// 获取Vector大小
size_t VectorSize(const Vector* vector);

// 获取Vector容量
size_t VectorCapacity(const Vector* vector);

// 清空Vector
void VectorClear(Vector* vector);

// 调整Vector容量
void VectorReserve(Vector* vector, size_t new_capacity);

// 确保Vector至少有min_capacity的容量
void VectorEnsureCapacity(Vector* vector, size_t min_capacity);

// 内部实现函数（不直接调用）
void VectorAddInt(Vector* vector, int value);
void VectorAddFloat(Vector* vector, float value);
void VectorAddDouble(Vector* vector, double value);
void VectorAddChar(Vector* vector, char value);
void VectorAddShort(Vector* vector, short value);
void VectorAddLong(Vector* vector, long value);
void VectorAddLongLong(Vector* vector, long long value);
void VectorAddUInt(Vector* vector, unsigned int value);
void VectorAddUChar(Vector* vector, unsigned char value);
void VectorAddUShort(Vector* vector, unsigned short value);
void VectorAddULong(Vector* vector, unsigned long value);
void VectorAddULongLong(Vector* vector, unsigned long long value);
void VectorAddBool(Vector* vector, int value);

// 获取元素的内部实现
int VectorGetInt(const Vector* vector, int index);
float VectorGetFloat(const Vector* vector, int index);
double VectorGetDouble(const Vector* vector, int index);
char VectorGetChar(const Vector* vector, int index);
short VectorGetShort(const Vector* vector, int index);
long VectorGetLong(const Vector* vector, int index);
long long VectorGetLongLong(const Vector* vector, int index);
unsigned int VectorGetUInt(const Vector* vector, int index);
unsigned char VectorGetUChar(const Vector* vector, int index);
unsigned short VectorGetUShort(const Vector* vector, int index);
unsigned long VectorGetULong(const Vector* vector, int index);
unsigned long long VectorGetULongLong(const Vector* vector, int index);
int VectorGetBool(const Vector* vector, int index);

// 设置元素的内部实现
void VectorSetInt(Vector* vector, int index, int value);
void VectorSetFloat(Vector* vector, int index, float value);
void VectorSetDouble(Vector* vector, int index, double value);
void VectorSetChar(Vector* vector, int index, char value);
void VectorSetShort(Vector* vector, int index, short value);
void VectorSetLong(Vector* vector, int index, long value);
void VectorSetLongLong(Vector* vector, int index, long long value);
void VectorSetUInt(Vector* vector, int index, unsigned int value);
void VectorSetUChar(Vector* vector, int index, unsigned char value);
void VectorSetUShort(Vector* vector, int index, unsigned short value);
void VectorSetULong(Vector* vector, int index, unsigned long value);
void VectorSetULongLong(Vector* vector, int index, unsigned long long value);
void VectorSetBool(Vector* vector, int index, int value);

// 删除元素
void VectorDelete(Vector* vector, int index);

// 删除指定范围的元素
void VectorDeleteRange(Vector* vector, int start, int end);

// 复制Vector
Vector* VectorCopy(const Vector* vector);

// 从数组创建Vector
Vector* VectorFromArray(VectorType type, const void* array, size_t count);

// 扩展Vector
void VectorExtend(Vector* vector, const Vector* extend_vector);

// 查找元素索引
int VectorIndex(const Vector* vector, const void* value);

// 检查元素是否存在
int VectorHas(const Vector* vector, const void* value);

// 计算元素出现次数
size_t VectorCountValue(const Vector* vector, const void* value);

// 删除第一个匹配的元素
int VectorRemove(Vector* vector, const void* value);

// 截取Vector
Vector* VectorSlice(const Vector* vector, int start, int end, int step);

// 获取最小值
void* VectorMinValue(const Vector* vector);

// 获取最大值
void* VectorMaxValue(const Vector* vector);

// 反转Vector
void VectorReverse(Vector* vector);

// 排序Vector（升序）
void VectorSort(Vector* vector);

// 打印Vector内容（用于调试）
void VectorPrint(const Vector* vector);

// 使用_Generic宏提供类型安全的接口
#define VectorAdd(v, val) _Generic((val), \
    int: VectorAddInt, \
    float: VectorAddFloat, \
    double: VectorAddDouble, \
    char: VectorAddChar, \
    short: VectorAddShort, \
    long: VectorAddLong, \
    long long: VectorAddLongLong, \
    unsigned int: VectorAddUInt, \
    unsigned char: VectorAddUChar, \
    unsigned short: VectorAddUShort, \
    unsigned long: VectorAddULong, \
    unsigned long long: VectorAddULongLong, \
    _Bool: VectorAddBool \
)(v, val)

// VectorGet需要根据vector类型动态选择适当的获取函数
// 由于C语言限制，我们使用一个辅助函数来处理类型选择
#define VectorGet(v, idx) _Generic((v), \
    Vector*: VectorGetByType(v, idx), \
    const Vector*: (const void*)VectorGetByType((Vector*)v, idx) \
)

// 声明辅助函数
void* VectorGetByType(Vector* vector, int index);

#define VectorSet(v, idx, val) _Generic((val), \
    int: VectorSetInt, \
    float: VectorSetFloat, \
    double: VectorSetDouble, \
    char: VectorSetChar, \
    short: VectorSetShort, \
    long: VectorSetLong, \
    long long: VectorSetLongLong, \
    unsigned int: VectorSetUInt, \
    unsigned char: VectorSetUChar, \
    unsigned short: VectorSetUShort, \
    unsigned long: VectorSetULong, \
    unsigned long long: VectorSetULongLong, \
    _Bool: VectorSetBool \
)(v, idx, val)

#endif // VECTOR_H